package com.ikdong.toodle.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.util.Log;
import com.ikdong.toodle.dao.FolderDAO;
import com.ikdong.toodle.exception.TDException;
import com.ikdong.toodle.model.Folder;

public class FolderManager {
	private FolderDAO fDao;
	private String userId;
	private String key;
	private TaskManager tm;
	
	public FolderManager(Context context){
		fDao = new FolderDAO(context);
		tm = new TaskManager(context);
		userId = new AccountManager(context).getAccount().getUserid();
		key = new AuthManager(context).getKey();
	}
	
	private void syncNew() throws Exception{
		List<Folder> folders = getFolderNew();
		Log.d(Constant.TAG, "syncLocalNew: "+folders.size());
		for(Folder folder : folders){
			try {
				Folder sf = ToodleHttpClient.addFolder(key,folder);
				if(sf!=null){
					fDao.replace(folder.getId().toString(), sf);
					tm.updateFolderChange(folder.getId(), sf.getId());
				}
			} catch (TDException e) {
				String code = e.getCode();
				if("5".equals(code)){
					
				}else if("6".equals(code)){
					
				}else{
					throw new TDException("100","Unknown Error.");
				}
			}
		}
	}
	
	private void syncDelete() throws Exception{
		List<Folder> folders = getFolderDelete();
		Log.d(Constant.TAG, "syncLocalDelete: "+folders.size());
		for(Folder folder : folders){
			try {
				ToodleHttpClient.deleteFolder(key,folder.getId());
				fDao.delete(folder.getId());
			} catch (TDException e) {
				Log.e(Constant.TAG, "syncDelete()", e);
				fDao.delete(folder.getId());
			}
		}
	}
	
	private void syncEdit() throws Exception{
		List<Folder> folders = getOnline();
		List<Long> ids = new ArrayList<Long>();
		Log.d(Constant.TAG, "syncLocalDelete: "+ids.size());
		for(Folder sFolder : folders){
			Long id = sFolder.getId();
			ids.add(id);
			Folder lFolder = fDao.get(id);
			if(lFolder==null){
				fDao.add(sFolder.getId(), sFolder.getName(), sFolder.getPrivated(), sFolder.getArchived(), sFolder.getOrder(), Constant.STATUS_SYNC_DONE, userId);
			}else if(lFolder.getStatus()==Constant.STATUS_SYNC_MODIFY){
				Folder f = ToodleHttpClient.editFolder(key, lFolder);
				f.setStatus(Constant.STATUS_SYNC_DONE);
				fDao.update(f);
			}else if(!lFolder.toString().equals(sFolder.toString())){
				fDao.update(sFolder);
			}
		}
		
		fDao.deleteWithoutIds(ids);
	}
	
	
	public void sync(boolean isSyncUpdate)throws Exception{
		syncNew();
		syncDelete();
		if(isSyncUpdate) 
			syncEdit();
	}
	
	public boolean add(Folder folder){
		boolean rlt = false;
		if(folder!=null){
			rlt = fDao.add(new Date().getTime(), 
					folder.getName(), 
					folder.getPrivated(), 
					folder.getArchived(), 
					new Date().getTime(), 
					Constant.STATUS_SYNC_ADD, 
					userId);
		}
		
		return rlt;
	}
	
	public boolean delete(long id){
		return fDao.delete(id);
	}
	
	public boolean deleteByStatus(long id){
		boolean rlt = false;
		if(userId!=null){
			rlt = fDao.updateStatus(id, Constant.STATUS_SYNC_DELETE);
			tm.updateFolderChange(id, 0);
		}else{
			rlt = fDao.delete(id);
		}
		
		return rlt;
	}
	
	public List<Folder> get(){
		List<Folder> folders = new ArrayList<Folder>();
		folders.addAll(fDao.get(null,userId,Constant.STATUS_SYNC_ADD,Constant.STATUS_SYNC_DONE,Constant.STATUS_SYNC_MODIFY));
		return folders;
	}
	
	public List<Folder> getUnArchived(){
		List<Folder> folders = new ArrayList<Folder>();
		folders.addAll(fDao.get(Constant.FOLDER_STATUS_UNARCHIVED,userId,Constant.STATUS_SYNC_ADD,Constant.STATUS_SYNC_DONE,Constant.STATUS_SYNC_MODIFY));
		return folders;
	}
	
	public String[] getFolderNames(){
		List<Folder> folders = this.get();
		List<String> names = new ArrayList<String>();
		names.add("All");
		for(Folder f : folders){
			names.add(f.getName());
		}
		names.add("No Folder");
		return names.toArray(new String[names.size()]);
	}
	
	public Folder get(long id){
		return fDao.get(id);
	}
	
	public Folder get(String name){
		return fDao.get(name);
	}
	
	public boolean update(Folder folder){
		boolean rlt = false;
		folder.setStatus(Constant.STATUS_SYNC_MODIFY);
		folder.setSig(userId);		
		rlt = fDao.update(folder);
		
		return rlt;
	}
	
	public void deleteOnline(long id){
		try {
			ToodleHttpClient.deleteFolder(key, id);
		} catch (TDException e) {
			Log.e(Constant.TAG, "deleteOnline()", e);
		}
	}
	
	public List<Folder> getFolderNew(){
		return fDao.get(null,userId, Constant.STATUS_SYNC_ADD);
	}
	
	public List<Folder> getFolderDelete(){
		return fDao.get(null,userId, Constant.STATUS_SYNC_DELETE);
	}
	
	public List<Folder> getFolderModify(){
		return fDao.get(null,userId, Constant.STATUS_SYNC_MODIFY);
	}
	
	public List<Folder> getOnline(){
		List<Folder> folders = new ArrayList<Folder>();
		folders.addAll(ToodleHttpClient.getFolders(key));
		return folders;
	}
}
